package es.udc.fbellas.j2ee.tiendaweb.http.controller.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.tiendaweb.http.controller.session.SessionManager;
import es.udc.fbellas.j2ee.tiendaweb.http.view.actionforms.UserProfileForm;
import es.udc.fbellas.j2ee.tiendaweb.http.view.applicationobjects.Countries;
import es.udc.fbellas.j2ee.tiendaweb.http.view.applicationobjects.Languages;
import es.udc.fbellas.j2ee.tiendaweb.model.direccionpostal.TO.DireccionPostalTO;
import es.udc.fbellas.j2ee.tiendaweb.model.tarjetabancaria.TO.TarjetaBancariaTO;
import es.udc.fbellas.j2ee.tiendaweb.model.usuario.TO.UsuarioProfileTO;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
    
public class EditUserProfileAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException, InternalErrorException {
        
        /* 
         * Insert list of languages and countries (ordered by current language)
         * as attributes in the "request".
         */
        request.setAttribute("languages", 
            Languages.getLanguages(getLocale(request).getLanguage()));
        request.setAttribute("countries", 
            Countries.getCountries(getLocale(request).getLanguage()));
        
        /* Fill "form". */
        UserProfileForm userProfileForm = (UserProfileForm) form;
        
        /*
         * If the request is to allow the user to correct errors in the form,
         * "userProfileForm" must not be modified.
         */         
        String action = userProfileForm.getAction();
                
        if (request.getAttribute(Globals.ERROR_KEY) == null) {
            
            /* 
             * Set "language" and "country" to his/her current selection. 
             */
            userProfileForm.setLanguage(getLocale(request).getLanguage());
            userProfileForm.setCountry(getLocale(request).getCountry());

            /*
             * If the user is updating his/her profile, set the rest of
             * attributes.
             */           
            if ("UPDATE".equals(action)) {
            
                UsuarioProfileTO usuarioProfileTO = 
                    SessionManager.getUsuarioFacadeDelegate(request).buscarUsuarioProfile();
                DireccionPostalTO direccionPostalTO = 
                    SessionManager.getUsuarioFacadeDelegate(request).buscarDireccionUsuario();
                TarjetaBancariaTO tarjetaBancariaTO = 
                    SessionManager.getUsuarioFacadeDelegate(request).buscarTarjetaUsuario();

                
                userProfileForm.setFirstName(usuarioProfileTO.getNombre());
                userProfileForm.setSurname(usuarioProfileTO.getApellidos());
                userProfileForm.setEmail(usuarioProfileTO.getEmail());
                userProfileForm.setCalle(direccionPostalTO.getCalle());
                userProfileForm.setNombreReceptor(direccionPostalTO.getNombreReceptor());
                userProfileForm.setCodPostal(direccionPostalTO.getCP());
                userProfileForm.setNumero(direccionPostalTO.getPortal());
                userProfileForm.setPuerta(direccionPostalTO.getPuerta());
                userProfileForm.setNumTarjeta(tarjetaBancariaTO.getNumTarjeta());
                userProfileForm.setFechaExp(tarjetaBancariaTO.getFecha().toString());
                                                                   
            }
            
        }
        
        /* Return ActionForward. */
        if ("UPDATE".equals(action)) {
            return mapping.findForward("UpdateUserProfileDetailsForm");
        } else {
            return mapping.findForward("RegisterUserForm");
        }
        
    }

}
